# -*- coding: utf-8 -*-#
#-------------------------------------------------------------------------------
# 建立者:        黄周云  
# Name:         test07
# Description:   例7.5
# Author:       ASUS
# Date:         2019/3/8
#-------------------------------------------------------------------------------


# 定义一个函数，该函数指定了一个可变参数
def addNumber(*numbers):
    result = 0
    for number in numbers:
        result += number
    return result
print(addNumber(1,2,3,4,5))
print("------------------------")
# 定义一个函数，在可变参数前加一个普通形参
def calculator(type,*numbers):
    result = 0
    #进行加法运算
    if type == "add":
        for number in numbers:
            result += number
    # 进行减法
    elif type == "sub":
        result = numbers[0]
        for i in range(1,len(numbers)):
            result -= numbers[i]
    #进行乘法
    elif type == "mul":
        result = 1
        for number in numbers:
            result *= number
    #进行除法
    else:
        result = numbers[0]
        for i in range(1,len(numbers)):
            result /=numbers[i]
    return result
print(calculator("add",1,2,3,4,5,6))
print(calculator("sub",1234,44,54,12,57))
print(calculator("mul",1,2,3,4,5,6,7))
print(calculator("div",100,2,5))
print("----------------------------------------")
# 定义一个函数，在指定参数后指定一个普通形参
def calculator1(type,*numbers,ratio):
    #再调用另一个函数时，如果为该函数的可变参数传入另一个可变参数，也要在参数名前加*
    return calculator(type,*numbers)*ratio
print(calculator1("add",1,2,3,4,5,6,ratio = 3))
print(calculator1("sub",1234,44,54,12,57,ratio = 2))
print(calculator1("mul",1,2,3,4,5,6,7,ratio = 4))
print(calculator1("div",100,2,5,ratio = 4))
print("---------------------------------------------------")
# 定义一个函数，为ratio参数指定一个默认值
def calculator2(type,*numbers,ratio = 4):
    return calculator(type, *numbers) * ratio
print(calculator2("add",1,2,3,4,5,6))







